<?php
namespace Admin\Controller;
class CateController extends CommonController {
	private $model;
	public function __auto() {
		$this -> model = new \Common\Model\Cate;
	}

	//分类列表的显示
	public function index() {
		$data = $this -> model ->orderBy('sort')->get();
//		p($data);die;
		View::with('data', $data);
		View::make();
	}

	//分类表的添加
	public function add() {
		$modele = new \Common\Model\Type;
		$typeData = $modele -> get();
		View::with('typeData', $typeData);
		$cateData=$this->model->get();
		$cateData=Data::tree($cateData,'cname');
		View::with('cateData',$cateData);
//		p($cateData);
		if (IS_POST) {
//			p($_POST);die;
			//如果没有pid证明是分类表的顶级添加
			if ($this -> model -> store()) {
				View::success('添加成功', U('Cate/index'));
			}
			View::error($this -> model -> getError());
			//			}

		}

		View::make();
	}

	public function addSon() {
		$pid = Q('get.pid', 0, 'intval');
		$tid=Q('get.tid',0,'intval');
		if (IS_POST) {
			if ($this -> model -> storeSon($pid,$tid)) {
				View::success('添加成功', U('Cate/index'));
			}
			View::error($this->model->getError());
		}
		View::make();

	}

	//分类表的删除
	public function del() {
		$cid = Q('get.cid', 0, 'intval');
		if ($this -> model -> del($cid)) {
			View::success('删除成功');
		}
		View::error($this -> model -> getError());
	}

	//分类表的修改
	public function edit() {
		$cid = Q('get.cid', 0, 'intval');
		$pid=Q('get.pid',0,'intval');
		$data = $this -> model -> getNoMy($cid,$pid);
		if(is_string($data)){
			$data=$data;
			View::with('data', $data);
		}else{
			$data=Data::tree($data,'cname');
			View::with('data', $data);
		}
		
		if (IS_POST) {
			//			p($_POST);die;
			if ($this -> model -> edit($cid)) {
				View::success('修改成功', U('Cate/index'));
			}
			View::error($this -> model -> getError());
		}

		View::make();
	}
	//排序
	public function sort(){
		$sort=$_POST['sort1'];
		$id=$_POST['cid'];
		$this->model->where(array('cid'=>$id))->save(array('sort'=>$sort));
	}

}
?>